{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from __future__ import print_function, absolute_import\n",
    "from gm.api import *\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "# 可以直接提取数据，掘金终端需要打开，接口取数是通过网络请求的方式，效率一般，行情数据可通过subscribe订阅方式\n",
    "# 设置token， 查看已有token ID,在用户-密钥管理里获取\n",
    "set_token('bf739ec28ad507f391a3c3e99ed0365007da88f8')\n",
    "# set_token('ca9c4532786a122aa5ceb3fd726432862e046f91')\n",
    "# data = stk_get_index_constituents(index='SHSE.000300')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取 2022-09-01 -- 2022-09-15 的主力合约：\n",
    "查询连续合约对应的真实合约，可期货主力连续、次主力连续、月份连续合约。"
   ]
  },
  {
   "cell_type": "code",
<<<<<<< HEAD
   "execution_count": 4,
=======
   "execution_count": 2,
>>>>>>> a0abd2434c6d200bd3a8bd3547dfc3efee7e6620
   "metadata": {},
   "outputs": [],
   "source": [
    "data = fut_get_continuous_contracts(csymbol='CZCE.CF', start_date=\"2024-07-01\", end_date=\"2024-08-15\")"
   ]
  },
  {
   "cell_type": "code",
<<<<<<< HEAD
   "execution_count": 6,
=======
   "execution_count": 3,
>>>>>>> a0abd2434c6d200bd3a8bd3547dfc3efee7e6620
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
<<<<<<< HEAD
       "[{'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-01'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-02'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-03'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-04'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-05'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-08'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-09'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-10'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-11'},\n",
       " {'symbol': 'SHFE.rb2410', 'trade_date': '2024-07-12'}]"
      ]
     },
     "execution_count": 6,
=======
       "[{'symbol': 'CZCE.CF409', 'trade_date': '2024-07-01'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-02'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-03'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-04'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-05'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-08'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-09'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-10'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-11'},\n",
       " {'symbol': 'CZCE.CF409', 'trade_date': '2024-07-12'}]"
      ]
     },
     "execution_count": 3,
>>>>>>> a0abd2434c6d200bd3a8bd3547dfc3efee7e6620
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'SHFE.rb2410'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0]['symbol']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "symbols = ['CZCE.SA','CZCE.MA'] # 订阅交易标的 \n",
    "main_contract = {symbol:fut_get_continuous_contracts(csymbol=symbol, \n",
    "                                                    start_date=\"2024-07-01\", \n",
    "                                                    end_date=\"2024-08-15\"                                                    \n",
    "                                                    )[0]['symbol'] \n",
    "                                                    for symbol in symbols}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CZCE.SA': 'CZCE.SA409', 'CZCE.MA': 'CZCE.MA409'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main_contract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "main_contract_list = list(main_contract.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CZCE.SA409', 'CZCE.MA409']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main_contract_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "symbol = ['CZCE.SA','CZCE.MA'] \n",
    "date =datetime.datetime.now().strftime('%Y-%m-%d')\n",
    "main_contract = {symbol:fut_get_continuous_contracts(csymbol=symbol, \n",
    "                                                     start_date=date, \n",
    "                                                     end_date=date)[0]['symbol'] \n",
    "                 for symbol in symbol}\n",
    "main_contract_list = list(main_contract.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'CZCE.SA': 'CZCE.SA409', 'CZCE.MA': 'CZCE.MA409'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "main_contract\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 螺纹钢主力历史数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#'300s'使用的频率，300s为5分钟bar\n",
    "history_data = history(symbol='SHFE.rb2410', frequency='300s', start_time='2024-07-1',  end_time='2024-07-31', fields='eob, open, close, low, high, volume', adjust=ADJUST_PREV, df=False)\n",
    "# 转换为 DataFrame\n",
    "df = pd.DataFrame(history_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>eob</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3537.0</td>\n",
       "      <td>3539.0</td>\n",
       "      <td>3533.0</td>\n",
       "      <td>3535.0</td>\n",
       "      <td>49886</td>\n",
       "      <td>2024-07-01 09:05:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3535.0</td>\n",
       "      <td>3537.0</td>\n",
       "      <td>3532.0</td>\n",
       "      <td>3537.0</td>\n",
       "      <td>25805</td>\n",
       "      <td>2024-07-01 09:10:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3537.0</td>\n",
       "      <td>3542.0</td>\n",
       "      <td>3536.0</td>\n",
       "      <td>3538.0</td>\n",
       "      <td>25009</td>\n",
       "      <td>2024-07-01 09:15:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3539.0</td>\n",
       "      <td>3543.0</td>\n",
       "      <td>3537.0</td>\n",
       "      <td>3543.0</td>\n",
       "      <td>21451</td>\n",
       "      <td>2024-07-01 09:20:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3543.0</td>\n",
       "      <td>3546.0</td>\n",
       "      <td>3541.0</td>\n",
       "      <td>3542.0</td>\n",
       "      <td>26654</td>\n",
       "      <td>2024-07-01 09:25:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410</th>\n",
       "      <td>3518.0</td>\n",
       "      <td>3518.0</td>\n",
       "      <td>3516.0</td>\n",
       "      <td>3516.0</td>\n",
       "      <td>6832</td>\n",
       "      <td>2024-07-08 22:45:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>411</th>\n",
       "      <td>3517.0</td>\n",
       "      <td>3517.0</td>\n",
       "      <td>3514.0</td>\n",
       "      <td>3516.0</td>\n",
       "      <td>4952</td>\n",
       "      <td>2024-07-08 22:50:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>412</th>\n",
       "      <td>3517.0</td>\n",
       "      <td>3518.0</td>\n",
       "      <td>3515.0</td>\n",
       "      <td>3517.0</td>\n",
       "      <td>6559</td>\n",
       "      <td>2024-07-08 22:55:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>413</th>\n",
       "      <td>3517.0</td>\n",
       "      <td>3518.0</td>\n",
       "      <td>3515.0</td>\n",
       "      <td>3516.0</td>\n",
       "      <td>11792</td>\n",
       "      <td>2024-07-08 23:00:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>414</th>\n",
       "      <td>3516.0</td>\n",
       "      <td>3517.0</td>\n",
       "      <td>3512.0</td>\n",
       "      <td>3512.0</td>\n",
       "      <td>21100</td>\n",
       "      <td>2024-07-09 09:05:00+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>415 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       open    high     low   close  volume                       eob\n",
       "0    3537.0  3539.0  3533.0  3535.0   49886 2024-07-01 09:05:00+08:00\n",
       "1    3535.0  3537.0  3532.0  3537.0   25805 2024-07-01 09:10:00+08:00\n",
       "2    3537.0  3542.0  3536.0  3538.0   25009 2024-07-01 09:15:00+08:00\n",
       "3    3539.0  3543.0  3537.0  3543.0   21451 2024-07-01 09:20:00+08:00\n",
       "4    3543.0  3546.0  3541.0  3542.0   26654 2024-07-01 09:25:00+08:00\n",
       "..      ...     ...     ...     ...     ...                       ...\n",
       "410  3518.0  3518.0  3516.0  3516.0    6832 2024-07-08 22:45:00+08:00\n",
       "411  3517.0  3517.0  3514.0  3516.0    4952 2024-07-08 22:50:00+08:00\n",
       "412  3517.0  3518.0  3515.0  3517.0    6559 2024-07-08 22:55:00+08:00\n",
       "413  3517.0  3518.0  3515.0  3516.0   11792 2024-07-08 23:00:00+08:00\n",
       "414  3516.0  3517.0  3512.0  3512.0   21100 2024-07-09 09:05:00+08:00\n",
       "\n",
       "[415 rows x 6 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
